package com.thti.mecool.client.activity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.thti.mecool.client.adapter.MyplanAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.send.plan.PlanSend;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.model.biz.TaskBizImpl;
import com.thti.mecool.client.model.po.PlanClient;
import com.thti.mecool.client.model.po.Task;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.ListWidget;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class MyPlanActivity extends BaseActivity{
	TextView title,reason;
	PlanBizImpl planBizImpl;
	LinearLayout reasonLinearLayout;
	ProgressDialogWidget progressDialogWidget;
	ListWidget listWidget;
	private ArrayList<ArrayList<HashMap<String, String>>> taskList;
	private ArrayList<HashMap<String, String>> projects;
	ArrayList<String> listTag;
	private int curPos;
	private ArrayList<String> taskids, projectids;
	public static Handler handler;
	TaskBizImpl taskBizImpl;
	List<Task> tasks;
	String planId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Cache.add(this);
		this.noTitle();
		setContentView(R.layout.mecool_myplan);
		initToolbar();
		init();
		
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch(msg.what){
				case 0xAA:
					progressDialogWidget.dismiss();
					getDataFromDb();
					break;
				case 0xBB:
					progressDialogWidget.dismiss();
					Util.sendToast(MyPlanActivity.this, "无数据");
					break;
				}
				super.handleMessage(msg);
			}
			
		};
		PlanSend.getMyPlan(getCurrentUser().getLoginName(), getCurDate());
		progressDialogWidget.setDismissTime(16);
		service();
	}
	
	
	private void init(){
		reasonLinearLayout=(LinearLayout)findViewById(R.id.mecool_myplan_reason_linearlayout);
		title=(TextView)findViewById(R.id.mecool_myplan_plan_title);
		reason=(TextView)findViewById(R.id.mecool_myplan_reason);
		taskList=new ArrayList<ArrayList<HashMap<String,String>>>();
		projects=new ArrayList<HashMap<String,String>>();
		listTag = new ArrayList<String>();
		taskids = new ArrayList<String>();
		projectids = new ArrayList<String>();
		progressDialogWidget=new ProgressDialogWidget(this);
		planBizImpl=new PlanBizImpl(this);
		taskBizImpl=new TaskBizImpl(this);
	}
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("我的计划");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.right_btn.setOnClickListener(rightbuttonListener);
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
		Toolbar.right_btn.setImageResource(R.drawable.mecool_plan_edit);
	}
	
	private void getDataFromDb() {
		String time=getCurDate();
		PlanClient planClient=planBizImpl.findByType(2, time,String.valueOf(getCurrentUser().getId()));
		Util.print("planClient>>>>"+planClient);
		planId=planClient.getPlanId();
		if(planClient.getStatus().equals("confirm")){
			reasonLinearLayout.setVisibility(View.GONE);
			title.setText("您的计划审核通过");
		}else if(planClient.getStatus().equals("deny")){
			reasonLinearLayout.setVisibility(View.VISIBLE);
			title.setText("您的计划被退回");
			reason.setText(planClient.getDenyNotes());
		}
		String planId=planClient.getPlanId();
		tasks=taskBizImpl.findByPlan(planId,String.valueOf(getCurrentUser().getId()));
		for (Task task : tasks) {			
			HashMap<String, String> Task = new HashMap<String, String>();
			projects = new ArrayList<HashMap<String, String>>();
			Task.put("name", task.getTaskName());
			Task.put("time", task.getStartTime() + " - " + task.getEndTime());
			Task.put("addr", task.getStoreName());
			taskids.add(task.getId() + "");
			projectids.add(task.getProjectId());
			listTag.add(task.getProjectName());
			projects.add(Task);
			taskList.add(projects);
		}
		listWidget= new ListWidget(MyPlanActivity.this, taskList, listTag);
	}
	
	private void service(){
	}
	
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
		}};
		
	private OnClickListener rightbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(tasks!=null){
				Intent intent=new Intent(MyPlanActivity.this,ChangePlanActivity.class);
				intent.putExtra("tasklist", (Serializable)tasks);
				intent.putExtra("planid", planId);
				intent.putExtra("isMyPlan", true);
				startActivity(intent);
			}
		}
		
	};


	private String getCurDate(){
		String date=null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date curDate = new Date(System.currentTimeMillis());
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(curDate);
		calendar.add(calendar.DATE, 1);
		date=sdf.format(calendar.getTime());
		return date;
	}
}
